package com.mjlf.messagepack.client;

import com.mjlf.messagepack.User;
import com.mjlf.messagepack.coderc.MessagePackDecoder;
import com.mjlf.messagepack.coderc.MessagePackEncoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;

import java.net.InetSocketAddress;

/**
 * @ClassName EchoClient
 * @auther mjlf
 * @Date 2019/9/23 17:18
 * @Description 使用 message pack 编码解密进行对象传递测试
 */
public class EchoClient {

    public static void main(String[] args) {
        EventLoopGroup work = new NioEventLoopGroup();

        try{
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(work)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()
                                    /**
                                     * lengthFieldOffset //长度字段的偏差
                                     * lengthFieldLength //长度字串的字节数
                                     * lengthAdjustment //添加到长度字符串的补偿值
                                     * initialBytesToStrip //从解码帧中第一次去除的字节数
                                     */
                                    .addLast("frameDecoder",new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8,0,8))
                                    .addLast(new MessagePackDecoder())
                                    .addLast("frameEncoder",new LengthFieldPrepender(8))
                                    .addLast(new MessagePackEncoder())
                                    .addLast(new EchoClientHandler());
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress("127.0.0.1", 9090)).sync();
            channelFuture.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            work.shutdownGracefully();
        }
    }

    static class EchoClientHandler extends SimpleChannelInboundHandler {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            for(int i = 0; i < 10; i ++){
                User user = new User();
                user.setAddress("beijing"+ i);
                user.setName("name" + i);

                ctx.channel().writeAndFlush(user);
            }
            System.out.println("-----------------send over-----------------");
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            System.out.println(cause.getMessage());
            ctx.close();
        }
    }
}
